﻿using Microsoft.Extensions.DependencyInjection;
using MyWpfFramework.Helper;
using MyWpfFramework.ViewModels;
using System.Configuration;
using System.Data;
using System.Windows;

namespace MyWpfFramework
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            //ChangeTheme("Light");
            var MainWindow = new MainWindow();
            MainWindow.ShowDialog();
            
        }

        public IServiceProvider Services { get; }
        public new static App Current => (App)Application.Current;

        private static IServiceProvider ConfigureServices()
        { 
            var services = new ServiceCollection();
            services.AddSingleton<ILogger, Logger>();
            services.AddSingleton<IDBoperation, DbOperation>();
            
            return services.BuildServiceProvider();
        }
        /// <summary>
        /// 切换主题
        /// </summary>
        /// <param name="themeName"></param>
        public void ChangeTheme(string themeName)
        {
            var resouorceDict = new ResourceDictionary();
            if (themeName == "Dark")
            {
                resouorceDict.Source = new Uri("pack://application:,,,/MyWpfFramework;component/ResourceDictionarys/DarkTheme.xaml");
            }
            else
            {
                resouorceDict.Source = new Uri("pack://application:,,,/MyWpfFramework;component/ResourceDictionarys/LightTheme.xaml");
            }
            //移除当前主题
            foreach (var dict in Current.Resources.MergedDictionaries)
            {
                if (dict.Source.ToString().Contains("LightTheme.xaml") || dict.Source.ToString().Contains("DarkTheme.xaml"))
                {
                    Current.Resources.MergedDictionaries.Remove(dict);
                        break;
                }
            }
            // 添加新主题
            Current.Resources.MergedDictionaries.Add(resouorceDict);
        }
    }

}
